package com.java110.mall.shop.api;

import com.alibaba.fastjson.JSONObject;
import com.java110.dto.shop.ShopGoodsDto;
import com.java110.po.shop.ShopGoodsPo;
import com.java110.mall.shop.bmo.shopGoods.IDeleteShopGoodsBMO;
import com.java110.mall.shop.bmo.shopGoods.IGetShopGoodsBMO;
import com.java110.mall.shop.bmo.shopGoods.ISaveShopGoodsBMO;
import com.java110.mall.shop.bmo.shopGoods.IUpdateShopGoodsBMO;
import com.java110.utils.util.Assert;
import com.java110.utils.util.BeanConvertUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping(value = "/shopGoods")
public class ShopGoodsApi {

    @Autowired
    private ISaveShopGoodsBMO saveShopGoodsBMOImpl;
    @Autowired
    private IUpdateShopGoodsBMO updateShopGoodsBMOImpl;
    @Autowired
    private IDeleteShopGoodsBMO deleteShopGoodsBMOImpl;

    @Autowired
    private IGetShopGoodsBMO getShopGoodsBMOImpl;

    /**
     * 微信保存消息模板
     *
     * @param reqJson
     * @return
     * @serviceCode /shopGoods/saveShopGoods
     * @path /app/shopGoods/saveShopGoods
     */
    @RequestMapping(value = "/saveShopGoods", method = RequestMethod.POST)
    public ResponseEntity<String> saveShopGoods(@RequestBody JSONObject reqJson) {

        Assert.hasKeyAndValue(reqJson, "scId", "请求报文中未包含scId");
        Assert.hasKeyAndValue(reqJson, "shopId", "请求报文中未包含shopId");
        Assert.hasKeyAndValue(reqJson, "communityId", "请求报文中未包含communityId");
        Assert.hasKeyAndValue(reqJson, "communityName", "请求报文中未包含communityName");
        Assert.hasKeyAndValue(reqJson, "startTime", "请求报文中未包含startTime");
        Assert.hasKeyAndValue(reqJson, "endTime", "请求报文中未包含endTime");


        ShopGoodsPo shopGoodsPo = BeanConvertUtil.covertBean(reqJson, ShopGoodsPo.class);
        return saveShopGoodsBMOImpl.save(shopGoodsPo);
    }

    /**
     * 微信修改消息模板
     *
     * @param reqJson
     * @return
     * @serviceCode /shopGoods/updateShopGoods
     * @path /app/shopGoods/updateShopGoods
     */
    @RequestMapping(value = "/updateShopGoods", method = RequestMethod.POST)
    public ResponseEntity<String> updateShopGoods(@RequestBody JSONObject reqJson) {

        Assert.hasKeyAndValue(reqJson, "scId", "请求报文中未包含scId");
        Assert.hasKeyAndValue(reqJson, "shopId", "请求报文中未包含shopId");
        Assert.hasKeyAndValue(reqJson, "communityId", "请求报文中未包含communityId");
        Assert.hasKeyAndValue(reqJson, "communityName", "请求报文中未包含communityName");
        Assert.hasKeyAndValue(reqJson, "startTime", "请求报文中未包含startTime");
        Assert.hasKeyAndValue(reqJson, "endTime", "请求报文中未包含endTime");
        Assert.hasKeyAndValue(reqJson, "scId", "scId不能为空");


        ShopGoodsPo shopGoodsPo = BeanConvertUtil.covertBean(reqJson, ShopGoodsPo.class);
        return updateShopGoodsBMOImpl.update(shopGoodsPo);
    }

    /**
     * 微信删除消息模板
     *
     * @param reqJson
     * @return
     * @serviceCode /shopGoods/deleteShopGoods
     * @path /app/shopGoods/deleteShopGoods
     */
    @RequestMapping(value = "/deleteShopGoods", method = RequestMethod.POST)
    public ResponseEntity<String> deleteShopGoods(@RequestBody JSONObject reqJson) {
        Assert.hasKeyAndValue(reqJson, "communityId", "小区ID不能为空");

        Assert.hasKeyAndValue(reqJson, "scId", "scId不能为空");


        ShopGoodsPo shopGoodsPo = BeanConvertUtil.covertBean(reqJson, ShopGoodsPo.class);
        return deleteShopGoodsBMOImpl.delete(shopGoodsPo);
    }

    /**
     * 微信删除消息模板
     *
     * @param communityId 小区ID
     * @return
     * @serviceCode /shopGoods/queryShopGoods
     * @path /app/shopGoods/queryShopGoods
     */
    @RequestMapping(value = "/queryShopGoods", method = RequestMethod.GET)
    public ResponseEntity<String> queryShopGoods(@RequestParam(value = "communityId") String communityId,
                                                 @RequestParam(value = "page") int page,
                                                 @RequestParam(value = "row") int row) {
        ShopGoodsDto shopGoodsDto = new ShopGoodsDto();
        shopGoodsDto.setPage(page);
        shopGoodsDto.setRow(row);
        shopGoodsDto.setCommunityId(communityId);
        return getShopGoodsBMOImpl.get(shopGoodsDto);
    }
}
